<?php
class ArticleController extends Zend_Controller_Action
{   
    
    public function listerarticlesAction()
    {
    	$lettres = new Table_Vips();
    	$lettrevip = $this->_request->getParam("lettrevip");
    	$list=$lettres->getNomsPrenomsByLettre($lettrevip);
    	$this->view->listervip=array('list' =>$list);    	 	 
    }
    
	public function getnumVipAction(){
    	return ($this->_request->getParam("numvip")==null)?0:($this->_request->getParam("numvip"));
    }
    
	public function listerarticleAction()
	    {
	    	$vips = new Table_Vips();
	    	$apoursujet = new Table_Apoursujet();
	    	$articles = new Table_Articles();
	    	$numVip = $this->getnumVipAction();
	    	$list = null;
	    	if($numVip==0){
	    		$vip=null;
	    		$listarticle=null;
	    	}
	    	else{
	    		$vip=$vips->getOneVip($numVip);
	    		$listnumArticle=$apoursujet->getArticleByVIP($numVip);
	    		foreach($listnumArticle as $numArticle){
	    			$listarticle[]=$articles->getArticleVip($numArticle->ARTICLE_NUMERO);
	    		}
	    	}
	    	foreach ($apoursujet->getAllVip() as $vipnum){
	    		$list[] = $vips->getOneVip($vipnum->VIP_NUMERO);
	    	}
	    	$this->view->listerarticle=array('listarticle' =>$listarticle, 'vip'=>$vip, 'list'=>$list);    	 	 
	    }
	    
	public function ajouterarticleAction()
	{
		$form = new Form_AjouterArticle();
		
		if ($this->getRequest()->isPost()){
    		if ($form->isValid($this->getRequest()->getPost())){
    			$valeurs=$this->getRequest()->getPost();
    			$titre = $valeurs["titre"];
    			$contenu = $valeurs["contenu"];
    			$exemplaire = $valeurs["exemplaire"];
    			$pagedeb = $valeurs["pagedeb"];
    			$numvip = $valeurs["num"];
    			
    			$titre = ($titre=="")?null:$titre;
    			$contenu = ($contenu=="")?null:$contenu;
    			$exemplaire = ($exemplaire=="")?null:$exemplaire;
    			$pagedeb = ($pagedeb=="")?null:$pagedeb;
    			$numexemplaire = null;
    			
    			if(!is_null($exemplaire)){
	    			$table_exemplaire = new Table_Exemplaire();
	    			$numexemplaire = $table_exemplaire->insertExemplaire($exemplaire);
    			}
    			
    			$article = new Table_Articles();
    			$result = $article->insertArticle($titre, $contenu, $numexemplaire, $pagedeb);
    			
    			$apoursujet = new Table_Apoursujet();
    			$apoursujet->insertArticle($numvip, $result);
    			
    			$comporte = new Table_Comporte();
    			$comporte->insertArticle($numvip, $result, 1);
    			
    			$this->_redirect('/');
    		}
    	}
		
		$this->view->form = $form;
	}
	
	public function supprimerarticleAction()
	{
		$form = new Form_SupprimerArticle();
		
		if ($this->getRequest()->isPost()){
    		if ($form->isValid($this->getRequest()->getPost())){
    			$valeurs=$this->getRequest()->getPost();
    			$numart = $valeurs["num"];
    			
    			$comporte = new Table_Comporte();
    			$comporte->deleteArticle($numart);
    			
    			$apoursujet = new Table_Apoursujet();
    			$apoursujet->deleteArticle($numart);
    			
    			$article = new Table_Articles();
    			$article->deleteArticle($numart);
    			
    			$this->_redirect('/');
    			
    		}
    	}
		
		$this->view->form = $form;
	}
	
	public function modifierarticleAction()
	{
		$form = new Form_ModifierArticle();
		
		if ($this->getRequest()->isPost()){
    		if ($form->isValid($this->getRequest()->getPost())){
    			$valeurs=$this->getRequest()->getPost();
    			if (isset($valeurs["submit"])){
    				$numart = $valeurs["num"];
    				$_SESSION['articlevip'] = $numart;
    				$form = new Form_ModifierArticleBis();
					$this->view->form = $form;
    			}else {
    				$newtitre = $valeurs["titre"];
    				$newcontenu = $valeurs["contenu"];
    				$newpagedeb = $valeurs["pagedeb"];
    				$articletable = new Table_Articles();
    				$articletable->updateArticle($newtitre, intval($newpagedeb), $newcontenu , $_SESSION['articlevip']);
    				$this->_redirect('/');
    			}    			
    		}
    	}
		
		$this->view->form = $form;
	}
	    
	static function listevipsAction()
	{
		$vip = new Table_Vips();
    	$listeVips=$vip->getNomPrenomVips();
    	return $listeVips;  
	}
	
	static function listeurarticlevipAction(){
		$apoursujet = new Table_Apoursujet();
		$vips = new Table_Vips();
		$listeVips=null;
		foreach ($apoursujet->getAllVip() as $vipnum){
	    		$listeVips[] = $vips->getOneVip($vipnum->VIP_NUMERO);
	    	}
	    return $listeVips;
	}
	
	static function listerarticlebyvipnum($numVip){
		$apoursujet = new Table_Apoursujet();
	    $articles = new Table_Articles();
		$listnumArticle=$apoursujet->getArticleByVIP($numVip);
    	foreach($listnumArticle as $numArticle){
    		$listarticle[]=$articles->getArticleVip($numArticle->ARTICLE_NUMERO);
    	}
    	return $listarticle;
	}
   
 	
}// fin classe
?>